﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Web.UI.WebControls;
using iLangl;
using Telerik.Web.UI;
using iLangl.Customer.DNN.Components;
using iLangl.Customer.DNN.Managers;
using iLangl.Customer;

namespace iLangl.Customer.DNN.Presenters
{
	public class TreeViewPresenter
	{
		DnnModuleBase _module = null;
		public TreeViewPresenter(DnnModuleBase module)
		{
			_module = module;
		}
		public void bindTree(DnnTreeView tree)
		{
			tree.Nodes.Clear();

			var rootNode = new RadTreeNode() { Text = _module.GetLocalizedString("ProjectRootNode") };
			rootNode.Expanded = true;
			rootNode.AllowEdit = false;
			rootNode.Selected = true;
			rootNode.EnableContextMenu = true;
			rootNode.ImageUrl = _module.GetIcon(IconManager.ModuleIcon);
			tree.Nodes.Add(rootNode);

			rootNode.Nodes.Add(GetStateNode("ProjectStateArchived", ProjectStatuses.Archived));
			rootNode.Nodes.Add(GetStateNode("ProjectStateCancelled", ProjectStatuses.Cancelled));
			rootNode.Nodes.Add(GetStateNode("ProjectStateConfiguration", ProjectStatuses.Configuration));
			rootNode.Nodes.Add(GetStateNode("ProjectStateFinished", ProjectStatuses.Finished));
			rootNode.Nodes.Add(GetStateNode("ProjectStateInProgress", ProjectStatuses.InProgress));
			rootNode.Nodes.Add(GetStateNode("ProjectStateReadyToStart", ProjectStatuses.ReadyToStart));
			rootNode.Nodes.Add(GetStateNode("ProjectStateUnknown", ProjectStatuses.Unknown));
			rootNode.Nodes.Add(GetStateNode("ProjectStateWaitingForQuota", ProjectStatuses.WaitingForQuota));

		}

		public void FillByStatus(String status, DnnTreeView tree)
		{
			CustomerProject[] projects = ProjectManagerDNN.Instance.GetProjectsByStatus(status);

			RadTreeNode node = tree.FindNodeByValue(status);

			foreach (CustomerProject project in projects)
			{
				var childnode = new RadTreeNode() 
				{ 
					Text = project.Name, 
					Value = project.Id, 
					ToolTip = project.Description,
					AllowEdit = false
				};
				node.Nodes.Add(childnode);
			}
		}

		protected RadTreeNode GetStateNode(String resourceKey, String state)
		{
			var node = new RadTreeNode() { Text = _module.GetLocalizedString(resourceKey) };
			node.Value = state;
			node.Expanded = true;
			node.AllowEdit = false;
			node.EnableContextMenu = true;

			return node;
		}
	}
}